package com.colorlight.cloud.xm.entity.event;

import com.colorlight.cloud.xm.api.XmEntityEvent;
import com.colorlight.cloud.xm.entity.po.SenderInfo;
import com.colorlight.iot.proto.xm.event.XmEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Demon 发送卡监控数据上报
 * SenderMonitorReportEvent = 1003
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SenderMonitorReport implements XmEntityEvent {
	/**
	 * 发送卡详情 Xm
	 */
	private List<SenderInfo> senderInfo;
	
	@Override
	public SenderMonitorReport byteConvertObject(ByteString bytes) {
		XmEvent.SenderMonitorReport senderMonitorReport = null;
		try {
			senderMonitorReport = XmEvent.SenderMonitorReport.parseFrom(bytes);
		} catch (InvalidProtocolBufferException e) {
			log.error("", e);
		}
		Objects.requireNonNull(senderMonitorReport);
		
		this.setSenderInfo(
				senderMonitorReport.getSenderInfoList().stream()
						.map(e -> new SenderInfo().formProto(e))
						.collect(Collectors.toList()));
		return this;
	}
}
